/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.tree;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.KeyNamePairs;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.TreeNodeChangeListener;
import han.bifang.gwt.client.common.TreeNodeContentListener;
import han.bifang.gwt.client.common.entity.Service;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.TreeListener;

/**
 * 
 * @Author Kent
 */
public class ServiceVersionTree extends Content implements TreeListener,TreeNodeChangeListener {
	private ServiceMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private TreeNodeContentListener listener = null;

	private GwtContext context = null;

	private Tree tree;

	private TreeItem selected;
	
	private ServiceVersionTreeItem root;
	
	private Service ser;

	
	public ServiceVersionTree(ServiceMessages cons,
			BifangGWTServiceAsync service, GwtContext context,Service s) {
		this.cons = cons;
		this.service = service;
		this.context = context;
		this.ser = s;
		tree = new Tree();
		tree.setTitle(cons.serviceDomainTree());
		tree.addTreeListener(this);
		initWidget(tree);

	}

	public void setTreeNodeContentListener(TreeNodeContentListener l) {
		this.listener = l;

	}

	private AsyncCallback<ResponseObject> getAppCallback = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			ArrayList ds = (ArrayList) res.getResponse();

			for (int i = 0; i < ds.size(); i++) {
				KeyNamePairs st = (KeyNamePairs) ds.get(i);
				ServiceVersionTreeItem item = new ServiceVersionTreeItem();
				item.setHTML(st.getName());
				//item.setId(st.getId());
				item.setKey(st.getId());
				//item.setParentId(st.getParentId());
				//item.setType(st.getType());
				item.setSubType(ServiceConstants.SERVICE_VERSION);
				// item.addItem("");

				root.addItem(item);

			}
			root.setState(true, false);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	

	public void onLoad() {
		root = new ServiceVersionTreeItem();
		
		root.setHTML(ser.getServiceName());
		root.setSelected(true);
		root.setKey(ser.getId());
		root.setSubType(ServiceConstants.SERVICE_ROOT);

		selected = root;

		
//		root.setState(true);
//
//		root.addItem("");
		tree.addItem(root);
		RequestObject req = new RequestObject();
		req.setRequest(ser.getId());
		req
				.setAction("han.bifang.gwt.server.action.service.GetServiceVersionTreeAction");
		service.call(req, getAppCallback);
		
	}

	public void onUnLoad() {
		tree = new Tree();
		selected = null;
	}

	public void onTreeItemSelected(TreeItem item) {
		if (!item.equals(selected))
			selected.setSelected(false);
		selected = item;
		this.listener.onTreeNodeSelected(item);

	}

	public void onTreeItemStateChanged(TreeItem item) {
		boolean open = item.getState();
		ServiceVersionTreeItem i = (ServiceVersionTreeItem) item;
		if (open) {
			item.setState(true, false);
			if (i.getSubType().equals(ServiceConstants.SERVICE_ROOT)) {
				item.removeItems();
				
				RequestObject req = new RequestObject();
				req.setRequest(i.getKey());
				req
						.setAction("han.bifang.gwt.server.action.service.GetServiceVersionTreeAction");
				service.call(req, getAppCallback);
			}
		} else {
			item.setState(false, false);

		}

	}

	public void onAddNode(TreeItem item) {
		
		if (selected == null)
			return;
		// Window.alert(dt.getSubType());
		selected.addItem(item);
		ServiceVersionTreeItem t = (ServiceVersionTreeItem)selected;
		if(t.getSubType().equals(ServiceConstants.SERVICE_ROOT)){
			t.setState(true, false);
			t.setSelected(false);
			item.setSelected(true);
		}
		selected.setSelected(false);
		selected = item;
		item.setSelected(true);
		//item.setSelected(true);
		//selected.setState(false,false);
		//selected.setState(true,true);

	}

	public void onModifyNode(TreeItem item) {
		if (selected == null)
			return;
		ServiceVersionTreeItem i = (ServiceVersionTreeItem) item;
		ServiceVersionTreeItem s = (ServiceVersionTreeItem) selected;

		s.setHTML(i.getHTML());
		s.setId(i.getId());
		s.setKey(i.getKey());
		s.setParentId(i.getParentId());
		s.setSubType(i.getSubType());
		s.setType(i.getType());

	}

	public void onRemoveNode(TreeItem item) {
		if (selected == null)
			return;
		selected.setSelected(false);
		TreeItem di = item.getParentItem();
		di.removeItem(item);
		selected = di;
		di.setSelected(true);
		
		// for (int i = 0, j = selected.getChildCount(); i < j; i++) {
		// DomainTreeItem c = (DomainTreeItem) selected.getChild(i);
		// if (c.getId().equals(di.getId())) {
		// selected.removeItem(c);
		// break;
		// }
		//
		// }

	}

}
